﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace AMSReportBuildService
{
    class AssetLessenXmlFile
    {
        /// <summary>
        /// 获取每年的总数
        /// </summary>
        /// <returns></returns>
        private DataSet CountOfYear(string strYear)
        {
            string sqlString = @"Select * from (select convert(char(4),b.[CreateDate],112) as Year,count(*) as Amount from   dbo.AmsWorkBill_WoCommand a 
 join dbo.AmsWorkBill_WoInfo b on a.WoCode=b.Code where b.TypeName='Scrap' or b.TypeName='Damage'
Group by convert(char(4),b.[CreateDate],112)) a  ";
            if (strYear.Length != 0)
                sqlString += " Where Year='" + strYear + "'";
            return DataAccess.GetData(sqlString);
        }

        /// <summary>
        /// 获取每个月的总数
        /// </summary>
        /// <param name="strYear"></param>
        /// <returns></returns>
        private DataSet CountOfMonth(string strYear)
        {
            string sqlString = @"Select * from (select convert(char(4),b.[CreateDate],112) as Year,SubString(convert(char(7),b.[CreateDate],112),5,2) as Month,count(*) as Amount from   dbo.AmsWorkBill_WoCommand a 
 join dbo.AmsWorkBill_WoInfo b on a.WoCode=b.Code where b.TypeName='Scrap' or b.TypeName='Damage'
Group by convert(char(4),b.[CreateDate],112),SubString(convert(char(7),b.[CreateDate],112),5,2) ) a ";
            if (strYear.Length != 0)
                sqlString += " Where Year='" + strYear + "'";
            sqlString += @" Order by Year,Month";
            return DataAccess.GetData(sqlString);
        }

        private string[] Month
        {
            get
            {
                string[] month = new string[12];
                month[0] = "一月";
                month[1] = "二月";
                month[2] = "三月";
                month[3] = "四月";
                month[4] = "五月";
                month[5] = "六月";
                month[6] = "七月";
                month[7] = "八月";
                month[8] = "九月";
                month[9] = "十月";
                month[10] = "十一月";
                month[11] = "十二月";
                return month;
            }
        }

        private void CreateGraphXmlFile(string path, bool isAsset, bool isGraph)
        {
            var myXml = new XmlDocument();

            //建立Xml的定义声明
            XmlDeclaration dec = myXml.CreateXmlDeclaration("1.0", "GB2312", null);
            myXml.AppendChild(dec);

            XmlElement rootNode = myXml.CreateElement("chart");
            rootNode.SetAttribute("caption", "资产减少报表");
            rootNode.SetAttribute("xAxisName", "年份");
            rootNode.SetAttribute("yAxisName", "数量");
            rootNode.SetAttribute("baseFontSize", "12");
            rootNode.SetAttribute("palette", "1");
            myXml.AppendChild(rootNode);

            if (isGraph)
            {
                XmlElement categoriesNode = myXml.CreateElement("categories");
                myXml.DocumentElement.AppendChild(categoriesNode);
                foreach (string month in Month)
                {
                    XmlElement categoryNode = myXml.CreateElement("category");
                    categoryNode.SetAttribute("label", month);
                    categoriesNode.AppendChild(categoryNode);
                }
            }
            else
            {
                XmlElement dataNode = myXml.CreateElement("data");
                dataNode.SetAttribute("name", "年份");
                myXml.DocumentElement.AppendChild(dataNode);
                foreach (string month in Month)
                {
                    XmlElement monthNode = myXml.CreateElement("month");
                    monthNode.SetAttribute("amount", month);
                    dataNode.AppendChild(monthNode);
                }
            }

            DataSet YearList = CountOfYear("");

            foreach (DataRow yearRow in YearList.Tables[0].Rows)
            {
                var i = 0;
                var j = 0;
                XmlElement dataNode = myXml.CreateElement("dataset");
                dataNode.SetAttribute("seriesName", yearRow["Year"].ToString());
                if (!isGraph)
                    dataNode.SetAttribute("amount", yearRow["Amount"].ToString());
                myXml.DocumentElement.AppendChild(dataNode);

                DataSet monthList = CountOfMonth(yearRow[0].ToString());
                var rowCount = monthList.Tables[0].Rows.Count;

                foreach (DataRow monthRow in monthList.Tables[0].Rows)
                {
                    j++;
                    foreach (string currentMonth in Month)
                    {

                        var strMonth = CaseStringToInt(currentMonth);
                        if (Convert.ToInt32(strMonth) > i)
                        {


                            XmlElement setNode = myXml.CreateElement("set");
                            if (monthRow["Month"].ToString() != strMonth)
                            {
                                setNode.SetAttribute("name", strMonth);
                                setNode.SetAttribute("value", "0");
                                setNode.SetAttribute("percent", "0.00%");
                                dataNode.AppendChild(setNode);
                            }
                            else
                            {

                                setNode.SetAttribute("name", monthRow["Month"].ToString());
                                setNode.SetAttribute("value", monthRow["Amount"].ToString());
                                string toolText;
                                if (isGraph)
                                {
                                    toolText = monthRow["Amount"] + "，" + (Convert.ToDouble(monthRow["Amount"].ToString()) / Convert.ToInt32(yearRow["Amount"].ToString())).ToString("0.00%");
                                    setNode.SetAttribute("tooltext", toolText);
                                }
                                else
                                {
                                    toolText = (Convert.ToDouble(monthRow["Amount"].ToString()) / Convert.ToInt32(yearRow["Amount"].ToString())).ToString("0.00%");
                                    setNode.SetAttribute("percent", toolText);
                                }
                                dataNode.AppendChild(setNode);
                                if (j < rowCount)
                                    break;
                            }

                        }
                    }
                    i = Convert.ToInt32(monthRow["Month"].ToString());
                }

            }
            if (File.Exists(path))
                File.Delete(path);
            myXml.Save(path);
        }

        private string CaseStringToInt(string month)
        {
            string currentMonth;
            switch (month)
            {
                case "一月":
                    currentMonth = "01";
                    break;
                case "二月":
                    currentMonth = "02";
                    break;
                case "三月":
                    currentMonth = "03";
                    break;
                case "四月":
                    currentMonth = "04";
                    break;
                case "五月":
                    currentMonth = "05";
                    break;
                case "六月":
                    currentMonth = "06";
                    break;
                case "七月":
                    currentMonth = "07";
                    break;
                case "八月":
                    currentMonth = "08";
                    break;
                case "九月":
                    currentMonth = "09";
                    break;
                case "十月":
                    currentMonth = "10";
                    break;
                case "十一月":
                    currentMonth = "11";
                    break;
                case "十二月":
                    currentMonth = "12";
                    break;
                default:
                    currentMonth = "01";
                    break;
            }
            return currentMonth;
        }

        /// <summary>
        /// 创建GrowthTrendOfAsset.xml
        /// </summary>
        /// <param name="path"></param>
        public void CreateAssetGraphXmlFile(string path)
        {
            CreateGraphXmlFile(path, true, true);
        }

        ///// <summary>
        ///// 创建GrowthTrendOfEquipment.xml
        ///// </summary>
        ///// <param name="path"></param>
        //public void CreateEquipmentGraphXmlFile(string path)
        //{
        //    CreateGraphXmlFile(path, false, true);
        //}

        /// <summary>
        /// 创建GrowthTrendOfAssettb.xml
        /// </summary>
        /// <param name="path"></param>
        public void CreateAssetListXmlFile(string path)
        {
            CreateGraphXmlFile(path, true, false);
        }

        ///// <summary>
        ///// 创建GrowthTrendOfEquipmenttb.xml
        ///// </summary>
        ///// <param name="path"></param>
        //public void CreateEquipmentListXmlFile(string path)
        //{
        //    CreateGraphXmlFile(path, false, false);
        //}
    }
}
